using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using UniversalAdminSystem.Application.Common.Interfaces;
using UniversalAdminSystem.Application.UserManagement.Interface;
using UniversalAdminSystem.Application.UserManagement.Service;
using UniversalAdminSystem.Infrastructure.Persistence.Transaction;
using UniversalAdminSystem.Application.Authentication.Interfaces;
using UniversalAdminSystem.Application.Authentication.Service;
using FluentValidation;
using UniversalAdminSystem.Application.PermissionManagement.Services;
using UniversalAdminSystem.Application.PermissionManagement.Interfaces;
using UniversalAdminSystem.Application.LogManagement.Interfaces;
using UniversalAdminSystem.Application.LogManagement.Services;
using UniversalAdminSystem.Application.SystemSettings.Interfaces;
using UniversalAdminSystem.Application.SystemSettings.Services;
using UniversalAdminSystem.Application.FileStorage.Interfaces;
using UniversalAdminSystem.Application.FileStorage.Services;
using UniversalAdminSystem.Application.AIQuestions.Interfaces;
using UniversalAdminSystem.Application.AIQuestions.Service;
using UniversalAdminSystem.Infrastructure.Configs;

namespace UniversalAdminSystem.Infrastructure.DependencyInject;

public static class AddApplicationService
{
    /// <summary>
    /// 自动扫描并批量注册服务接口及其实现（支持接口和实现分布在不同程序集，约定接口名为IXXXService，实现名为XXXService）
    /// </summary>
    public static IServiceCollection AddServiceByConvention(
        this IServiceCollection services,
        Assembly[] interfaceAssemblies,
        Assembly[] implementationAssemblies)
    {
        var interfaces = interfaceAssemblies
            .SelectMany(a => a.GetTypes())
            .Where(t => t.IsInterface && t.Name.EndsWith("Service"));
        var implementations = implementationAssemblies
            .SelectMany(a => a.GetTypes())
            .Where(t => t.IsClass && !t.IsAbstract && t.Name.EndsWith("Service"));

        foreach (var iface in interfaces)
        {
            var impl = implementations.FirstOrDefault(t => iface.IsAssignableFrom(t));
            if (impl != null)
            {
                services.AddScoped(iface, impl);
            }
        }
        return services;
    }

    public static IServiceCollection AddApplication(this IServiceCollection services, IConfiguration configuration)
    {
        // 自动注册应用服务
        services.AddServiceByConvention(
            new Assembly[]
            {
                typeof(IUserManagementAppService).Assembly,
                typeof(ILoginAppService).Assembly,
                typeof(IFileAppService).Assembly,
                typeof(ILogManagementAppService).Assembly,
                typeof(ISystemSettingAppService).Assembly,
                typeof(IPermissionManagementAppService).Assembly,
                typeof(IAIQusetionsAppService).Assembly
            },
            new Assembly[]
            {
                typeof(UserManagementAppService).Assembly,
                typeof(LoginAppService).Assembly,
                typeof(FileAppService).Assembly,
                typeof(LogManagementAppService).Assembly,
                typeof(SystemSettingAppService).Assembly,
                typeof(PermissionManagementAppService).Assembly,
                typeof(AIQusetionsAppService).Assembly
            }
        );

        // 手动注册应用服务（确保所有服务都被注册）
        services.AddScoped<IUserManagementAppService, UserManagementAppService>();
        services.AddScoped<ILoginAppService, LoginAppService>();
        services.AddScoped<IRegisterAppService, RegisterAppService>();
        services.AddScoped<IFileAppService, FileAppService>();
        services.AddScoped<ILogManagementAppService, LogManagementAppService>();
        services.AddScoped<ISystemSettingAppService, SystemSettingAppService>();
        services.AddScoped<IPermissionManagementAppService, PermissionManagementAppService>();
        services.AddScoped<IPermissionCheckService, PermissionCheckService>();
        services.AddScoped<IRoleManagementAppService, RoleManagementAppService>();
        
        // 注册用户权限集成服务
        // services.AddScoped<UserPermissionIntegrationService>();
        
        // 注册JWT配置
        services.Configure<JwtConfig>(configuration.GetSection("Jwt"));
        
        // 注册工作单元
        services.AddScoped<IUnitOfWork, UnitOfWork>();
        
        return services;
    }
}